//
//  AllButtonViewController.m
//  HappinessTrip
//
//  Created by dllo on 15/10/9.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "AllButtonViewController.h"
#import "GetDataMethods.h"
#import "AllButtonCollectionViewCell.h"
#import "AllButton.h"
#import "AllPushViewController.h"
#import "MJRefresh.h"
#import "MBProgressHUD.h"
#import "Model.h"
#import "Fit.h"

@interface AllButtonViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)UICollectionView *collectionView;
@property(nonatomic, retain)NSMutableArray *allArray;
@property(nonatomic, assign)NSInteger number;
@property(nonatomic, retain)MBProgressHUD *progressView;

@end

@implementation AllButtonViewController
-(void)dealloc
{
    [_collectionView release];
    [_allArray release];
    [_progressView release];
    [super dealloc];
}

-(void)changeColor{
    self.collectionView.backgroundColor = [Model shareInstance].BackgroundColor;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
    self.navigationController.navigationBar.barTintColor = [Model shareInstance].BackgroundColor;
    [_collectionView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.allArray = [NSMutableArray array];
    _number = 0;
    [self reloadDataAndRefresh];
    [self changeColor];
    [self getData];
}

-(void)loadView
{
    [super loadView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor) name:@"changeColor" object:nil];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(170 * W, 220 * H);
    flowLayout.minimumLineSpacing = 10 * H;
    flowLayout.minimumInteritemSpacing = 10 * W;
    flowLayout.sectionInset = UIEdgeInsetsMake(10 * H, 10 * W, 10 * H, 10 * W);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 113) collectionViewLayout:flowLayout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [_collectionView registerClass:[AllButtonCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
    [self.view addSubview:_collectionView];
    [_collectionView release];
    
    //初始化加载中视图
    self.progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"玩命加载中";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    _progressView.backgroundColor = [UIColor colorWithRed:0.73 green:0.76 blue:0.77 alpha:.3];
    [self.view addSubview:_progressView];
    [_progressView release];
    
}

-(void)getData
{
    [GetDataMethods getDataByString:[NSString stringWithFormat:@"http:api.breadtrip.com/v2/new_trip/spot/hot/list/?start=%ld", _number] BodyString:nil WithDataBlock:^(id data) {
        NSDictionary *dataDic = [data objectForKey:@"data"];
        NSArray *array = [dataDic objectForKey:@"hot_spot_list"];
        for (NSDictionary *dic in array) {
            AllButton *all = [[AllButton alloc]init];
            [all setValuesForKeysWithDictionary:dic];
            [_allArray addObject:all];
        }
        [_collectionView reloadData];
        [_collectionView.header endRefreshing];
        [_collectionView.footer endRefreshing];
        [_progressView removeFromSuperview];
    }];
}

-(void)reloadDataAndRefresh
{
    _collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_allArray removeAllObjects];
        _number = 0;
        [self getData];
    }];
    _collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _number += 12;
        [self getData];
    }];
    
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AllButtonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    if (_allArray.count != 0) {
        AllButton *all = _allArray[indexPath.row];
        cell.allButton = all;
    }
    return cell;
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _allArray.count;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AllPushViewController *pushVC = [[AllPushViewController alloc]init];
    if (_allArray.count != 0) {
        pushVC.spot_id = [_allArray[indexPath.row] spot_id];
    }
    [self.navigationController pushViewController:pushVC animated:YES];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
